package com.ssi.myshop.ui.fragment;


import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.ssi.myshop.Presenter.impl.ICategoryPresenterImpl;
import com.ssi.myshop.R;
import com.ssi.myshop.base.BaseFragment;
import com.ssi.myshop.bean.CategoryBean;
import com.ssi.myshop.bean.CategoryContentBean;
import com.ssi.myshop.ui.DetailActivity;
import com.ssi.myshop.ui.adapter.CategoryLeftAdapter;
import com.ssi.myshop.ui.adapter.CategoryRightAdapter;
import com.ssi.myshop.utils.PresenterManager;
import com.ssi.myshop.view.ICategoryCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends BaseFragment implements ICategoryCallBack, CategoryLeftAdapter.OnLeftItemClickListener, CategoryRightAdapter.OnRightItemClickListener {

    private static final String TAG = "CategoryFragment";
    List<CategoryBean> mCategoryBeanList = new ArrayList<>();

    private ICategoryPresenterImpl mCategoryPresenter;

    @BindView(R.id.left_category_list)
    RecyclerView leftList;

    @BindView(R.id.right_content_list)
    RecyclerView rightList;

    private CategoryLeftAdapter mCategoryLeftAdapter;
    private CategoryRightAdapter mCategoryRightAdapter;


    public CategoryFragment() {
        // Required empty public constructor
    }

    @Override
    protected View lodeRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_lwith_bar_ayout, container, false);
    }

    @Override
    protected int getRootViewId() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initView(View rootView) {
        Log.d(TAG, "initView: "+rootView.getWidth());
        mCategoryLeftAdapter = new CategoryLeftAdapter();
        mCategoryLeftAdapter.setOnLeftItemClickListener(this);
        leftList.setLayoutManager(new LinearLayoutManager(getContext()));
        leftList.setAdapter(mCategoryLeftAdapter);
        mCategoryRightAdapter = new CategoryRightAdapter();
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rightList.setLayoutManager(gridLayoutManager);
        rightList.setAdapter(mCategoryRightAdapter);
        mCategoryRightAdapter.setOnRightItenClick(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initPresenter() {
        mCategoryPresenter = PresenterManager.getInstance().getCategoryPresenter();
        mCategoryPresenter.registerViewCallBack(this);
    }

    @Override
    protected void loadData() {
        mCategoryPresenter.getCategory();
    }


    @Override
    public void onCategoryLoad(List<CategoryBean> date) {
        setUpState(State.SUCCESS);
        Log.d(TAG, "onCategoryLoad: " + date.toString());
        mCategoryBeanList = date;
        mCategoryLeftAdapter.setCategoryData(date);
    }

    @Override
    public void onContentLoad(List<CategoryContentBean> data) {
        setUpState(State.SUCCESS);
        Log.d(TAG, "onContentLoad: " + data.size());
        mCategoryRightAdapter.setData(data);
    }

    @Override
    public void onError(String s) {

    }

    @Override
    public void onLoading() {
        if (mCategoryBeanList.size() == 0) {
            setUpState(State.LOADING);
        }
    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onSuccess() {

    }

    @Override
    protected void release() {
        super.release();
        mCategoryPresenter.unregisterViewCallBack(this);
    }

    @Override
    public void onLeftItemClick(CategoryBean bean) {
        Log.d(TAG, "onLeftItemClick: " + bean.toString());
        mCategoryPresenter.getContentByCategoryId(bean);
    }

    @Override
    public void onRightItemClick(CategoryContentBean bean) {
        Log.d(TAG, "onRightItemClick: " + bean.getId());
        Intent intent = new Intent();
        intent.putExtra("datail", bean);
        intent.setClass(getContext(), DetailActivity.class);
        getContext().startActivity(intent);
    }
}
